﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine.Networking;

public class AssetsPool:Singleton<AssetsPool>
{
    private static readonly object _syslock = new object();
    private Dictionary<string, AudioClip> dicAudioClip;//缓存
    private Dictionary<string, GameObject> dicGameObject;//缓存
    private Dictionary<string, AssetBundle> dicAssetBundle;
    private Dictionary<string, Material> dicMaterial;
    private Dictionary<string, object> dicObject;

    public AssetsPool()
    {
        dicAudioClip = new Dictionary<string, AudioClip>();
        dicGameObject = new Dictionary<string, GameObject>();
        dicAssetBundle = new Dictionary<string, AssetBundle>();
        dicObject = new Dictionary<string, object>();
        dicMaterial = new Dictionary<string, Material>();
    }
    
    /// <summary>
    /// 从资源池中加载物体
    /// </summary>
    /// <param name="PerfabeName"></param>
    /// <param name="ResourcesPath"></param>
    /// <returns></returns>
    public GameObject LoadGameObject(string PerfabeName,string ResourcesPath = "")
    {
        if (dicGameObject.ContainsKey(PerfabeName))//如果对象池里有这个文件
        {
            Debug.Log("从对象池中加载预制体");
        }
        else
        {
            GameObject obj = Resources.Load<GameObject>(ResourcesPath+ PerfabeName);
            dicGameObject.Add(PerfabeName, obj);
        }
        return dicGameObject[PerfabeName];
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="AudioName"></param>
    /// <returns></returns>
    public AudioClip LoadAudio(string AudioName)
    {
        if (dicAudioClip.ContainsKey(AudioName))//如果对象池里有这个文件
        {
            Debug.Log("从资源池中加载音频");
        }
        else
        {
            AudioClip audioClip = Resources.Load<AudioClip>("Sounds/" + AudioName);
            dicAudioClip.Add(AudioName, audioClip);
        }
        return dicAudioClip[AudioName];
    }
    
    /// <summary>
    /// 加载AB包中的某个物体到场景中
    /// </summary>
    /// <param name="ULR">AB包地址</param>
    /// <param name="PerfabePath"></param>
    /// <returns></returns>
    public IEnumerator InstantiatePerfabeFromeABAsy(string ULR, string PerfabePath,Vector3 posion,Quaternion rotation,Transform Part = null)
    {
#if UNIYU_2018
         UnityWebRequest WebRequest = UnityWebRequestAssetBundle.GetAssetBundle(ULR);

#else
        UnityWebRequest WebRequest = UnityWebRequestAssetBundle.GetAssetBundle(ULR);
#endif
        yield return WebRequest.SendWebRequest();
        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(WebRequest);
        var loadAsset = bundle.LoadAssetAsync<GameObject>(PerfabePath);
        yield return loadAsset;
        GameObject obj = loadAsset.asset as GameObject;
        if (dicGameObject.ContainsKey(obj.name))
        {
            //如果之前有这个资源就不放入集合中
        }
        else
        {
            dicGameObject.Add(obj.name, obj);
        }
        if (Part == null)
        {
            GameObject.Instantiate(dicGameObject[obj.name], posion, rotation);
        }
        else
        {
            GameObject.Instantiate(dicGameObject[obj.name], posion, rotation, Part);
        }
    }
    /// <summary>
    /// 加载材质资源池
    /// </summary>
    /// <returns></returns>
   public Material LoadMaterialToPool(string ResPath)
    {
        if (dicGameObject.ContainsKey(ResPath))//如果对象池里有这个文件
        {
            Debug.Log("从对象池中加载预制体");
        }
        else
        {
            Material obj = Resources.Load<Material>(ResPath);
            dicMaterial.Add(ResPath, obj);
        }
        return dicMaterial[ResPath];
    }

    /// <summary>
    /// 同步加载AB包中的音频文件
    /// </summary>
    /// <param name="PrefabeName">音频资源的名称</param>
    /// <param name="audioPath">AB包的路径</param>
    /// <returns></returns>
    public AudioClip LoadABAudioFile(string AudioName, string audioPath)
    {
        if (dicAudioClip.ContainsKey(AudioName))
        {
            return dicAudioClip[AudioName];
        }
        else
        {
            AssetBundle assetBundle = AssetBundle.LoadFromFile(audioPath);
            AudioClip audioClip = assetBundle.LoadAsset<AudioClip>(AudioName);
           dicAudioClip.Add(audioClip.name, audioClip);
            Debug.Log("AssetName:"+ audioClip.name);
            assetBundle.Unload(false);
            return dicAudioClip[AudioName];
        }
    }
    /// <summary>
    ///同步加载AB包 并缓在资源池中
    /// </summary>
    /// <param name="PerfabeName">AB包名</param>
    /// <param name="ABPath">AB包路径</param>
    /// <returns></returns>
    public GameObject LoadABGameObject(string PerfabeName, string ABPath)
    {
        if (dicGameObject.ContainsKey(PerfabeName))
        {
            return dicGameObject[PerfabeName];
        }
        else
        {
            AssetBundle assetBundle = AssetBundle.LoadFromFile(ABPath);
            GameObject gameObject = assetBundle.LoadAsset<GameObject>(PerfabeName);
            dicGameObject.Add(gameObject.name, gameObject);
            Debug.Log("AssetName:" + gameObject.name);
            assetBundle.Unload(false);
            return dicGameObject[PerfabeName];
        }
    }

}

